package com.apache.rpc.util;

import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import jxl.common.Logger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 
 * description:  加密工具类（desSec）
 * @author Hou Dayu 创建时间：2014-11-27  
 * @Copyright(c)2014:
 */
public class DesUtils {

	private Logger log = Logger.getLogger(DesUtils.class);
	private static String strDefaultKey = "";
	private static DesUtils instance = null;

	public static DesUtils getInstance() {
		if (instance == null) {
			instance = new DesUtils();
		}
		return instance;
	}

	private DesUtils() {
		strDefaultKey = ToolsUtil.isNull(ToolsUtil.getInstance().getValueByKey("desSecKey"), "12345678");
	}

	public byte[] desEncrypt(byte[] plainText) throws Exception {
		SecureRandom sr = new SecureRandom();
		DESKeySpec dks = new DESKeySpec(strDefaultKey.getBytes("UTF-8"));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, key, sr);
		byte data[] = plainText;
		byte encryptedData[] = cipher.doFinal(data);
		return encryptedData;
	}

	public byte[] desDecrypt(byte[] encryptText) throws Exception {
		SecureRandom sr = new SecureRandom();
		DESKeySpec desKeySpec = new DESKeySpec(strDefaultKey.getBytes("UTF-8"));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(desKeySpec);
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.DECRYPT_MODE, key, sr);
		byte encryptedData[] = encryptText;
		byte decryptedData[] = cipher.doFinal(encryptedData);
		return decryptedData;
	}

	public String encrypt(String input) {
		String result = "";
		try {
			result = base64Encode(desEncrypt(input.getBytes()));
		} catch (Exception e) {
			log.error(e);
		}
		return result;
	}

	public String decrypt(String input) {
		String result = "";
		try {
			byte[] results = base64Decode(input);
			result = new String(desDecrypt(results));
		} catch (Exception e) {
			log.error(e);
		}
		return result;
	}

	public static String base64Encode(byte[] s) {
		if (s == null)
			return null;
		BASE64Encoder b = new sun.misc.BASE64Encoder();
		return b.encode(s);
	}

	public static byte[] base64Decode(String s) throws IOException {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] b = decoder.decodeBuffer(s);
		return b;
	}

	public static void main(String[] args) {
		try {
			String test = "1234";
			//			DesUtils des = new DesUtils();
			//			//			25f9fb655120267f72786d42de8ba6c1
			//			System.out.println("加密前的字符：" + test);
			//			System.out.println("加密后的字符：" + des.encrypt(test, "apache"));
			//			System.out.println("解密后的字符：" + des.decrypt(des.encrypt(test, "apache")));

			String code = DesUtils.getInstance().encrypt(test);
			System.out.println("加密后的字符：" + code);
			code = DesUtils.getInstance().decrypt(code);
			System.out.println("解密后的字符：" + code);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
